#include"Buffer.h"
#include<string.h>
Buffer::Buffer(int size){
    this->m_capacity=size;
    this->m_readPos=this->m_writePos=0;
    this->m_data=new char[size]();
}

Buffer::~Buffer(){
    delete [] this->m_data;
}

//剩余未读的内存
int Buffer::readablesize(){
    return this->m_writePos-this->m_readPos;
}
//剩余能写的内存
int Buffer::writeablesize(){
    return this->m_capacity-this->m_writePos;
}

//扩容
void Buffer::MoveRoom(int size){
        //1.当内存足够
    if(writeablesize()>=size){
        return;
    }//2.当可写的内存需要 已读的内存加上可写的内存
    else if(this->m_readPos+writeablesize()>=size){ 
        //得到未读内存的大小
        int noreadable=readablesize();
        //将未读的内存转移到已读的内存空间去
        memcpy(this->m_data,this->m_data+this->m_readPos,noreadable);
        //更新位置
        this->m_readPos=0;
        this->m_writePos=noreadable;
    }//3.当内存空间不够  扩容
    else{ 
      void*temp=realloc(this->m_data,this->m_capacity+size);
      if(temp==NULL){
         return ;  //扩容失败
      }
      memset((char*)temp+this->m_capacity,0,size);
      //更新数据
      this->m_data=(char*)temp;
      this->m_capacity+=size;
    }
}
//写内存 1.直接写 2.接收套接字数据
int Buffer::AppendData(const char*data,int size){
    if(data==NULL){
        return -1;
    }
    //扩容
    this->MoveRoom(size);
    //将数据拷贝
    memcpy(this->m_data+this->m_writePos,data,size);
    this->m_writePos+=size;
    return 0;
}
int Buffer::AppendString(const char*data){
    int size=strlen(data);
    AppendData(data,size);
    return 0;
}
int Buffer::SocketRead(int fd){
    struct iovec vec[2];
    //初始化数组元素
    int writeable=writeablesize();  //可写元素个数
    vec[0].iov_base=this->m_data+this->m_writePos;
    vec[0].iov_len=writeable;

    char*tempbuf=new char[40960]();
    vec[1].iov_base=tempbuf;
    vec[1].iov_len=40960;
    int result=readv(fd,vec,2);
    if(result==-1){
        return -1;
    }else if(result<=writeable){
       this->m_writePos+=result;
    }else{
       //读到的数据超过缓冲区 将溢出的数据存储到tempbuf中去 
       //在通过函数bufferAppendData将tempbuf中的数据读进buf中
       this->m_writePos=this->m_capacity;
       AppendData(tempbuf,result-writeable);
    }
    //释放资源
    delete []tempbuf;
    return result;
}
//发送数据
int Buffer::SendData(int socket){
    int readable=readablesize();
      if(readable>0){
        int count=send(socket,this->m_data+this->m_readPos,readable,0);
        if(count){
            this->m_readPos+=count;
        }
        return count;
      }
      return 0;
}
//根据\r\n取出一行，找到其在数据块的位置，返回该值
char*Buffer::FindCRLF(){
    /*大数据块中指定数据块(需要指定数据块的大小)*/
    /*void *memmem(const void *haystack, size_t haystacklen,const void *needle, size_t needlelen);*/
#if 0
    char*ptr=(char*)memmem(this->m_data+this->m_readPos,readablesize(),":",1);
    return ptr;
#else
    char*ptr=strstr(this->GetData()+this->GetReadPos(),"\\r\\n");
    return ptr;
#endif
}
char*Buffer::GetData(){  //获取数据
    return this->m_data;
}
int Buffer::GetReadPos(){  //获取已读的位置
    return this->m_readPos;
}
int Buffer::GetWritePos(){  //获取已写的位置
    return this->m_writePos;
}
int Buffer::GetCapacity(){  //获取容量
    return this->m_capacity;
} 
void Buffer::readPosIncrease(int size){
    this->m_readPos+=size;  
}
void Buffer::writePosIncreasw(int size){
    this->m_writePos+=size;
}

void log(string str){
   if(OPEN){
     cout<<str<<endl;
   }
}
